/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src;

import java.util.*;
import java.sql.Date;
import beans.*;
import bdd.daos.impl.*;
/**
 *
 * @author videlcro
 */
public class PlanningJour {
    private int jour;
    private int mois;
    private int annee;
	private int formation;
	private int promotion;
    public List<String> creneau;

    public PlanningJour(int jour, int mois, int annee, int formation, int promotion) {
        super();
        this.jour = jour;
        this.mois = mois;
        this.annee = annee;
		this.formation = formation;
		this.promotion = promotion;
        this.creneau = new ArrayList();
		try
		{
		this.initialiser();
		this.remplir();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
    }

	/*public PlanningJour(int jour, int mois, int annee, String formation, int promotion)
	{
        super();
        this.jour = jour;
        this.mois = mois;
        this.annee = annee;
		this.formation = formation;
		this.promotion = promotion;
        this.creneau = new ArrayList();
		//TODO
		//Appeler bean pour obtenir les créneaux
		this.initialiser();
		List<Creneau> lc = new ArrayList();
		lc.add(new Creneau(null, new Jours(42), new Type(1,"TD"), new Enseignant(0, new Typeposte(),new Role(),"panpan", "lapin", "nanterre", 0,new Date(), "panpan", "panpan"),new Ec(01, "rien", 3), new Salle(),(short) 700,(short) 845));
		lc.add(new Creneau(null, new Jours(42), new Type(1,"TD"), new Enseignant(0, new Typeposte(),new Role(),"panpan2", "lapin2", "nanterre2", 0,new Date(), "panpan2", "panpan2"),new Ec(01, "rien2", 3), new Salle(),(short) 1000,(short) 1100));
		lc.add(new Creneau(null, new Jours(42), new Type(1,"TD"), new Enseignant(0, new Typeposte(),new Role(),"panpan3", "lapin3", "nanterre3", 0,new Date(), "panpan3", "panpan3"),new Ec(01, "rien3", 3), new Salle(),(short) 1300,(short) 1330));
		lc.add(new Creneau(null, new Jours(42), new Type(1,"TD"), new Enseignant(0, new Typeposte(),new Role(),"panpan4", "lapin4", "nanterre4", 0,new Date(), "panpan4", "panpan4"),new Ec(01, "rien4", 3), new Salle(),(short) 1500,(short) 1545));
		this.remplir(lc);
    }*/

    public final void initialiser()
	{
        for(int i = 0; i<48; i++)
		{
			this.creneau.add("");
		}
    }

	public final void remplir()
	{
		//List<Creneau> lc = new ArrayList();
		//appeler beans pour remplir lc
		
		String heureDeb = null;
		String heureFin = null;
		int caseDeb = 0;
		int caseFin = 0;
		String libelle = null;
		//appel dans la base
		DAOCreneauImpl crenImpl = new DAOCreneauImpl();

		Date datecren = new Date(this.annee, (this.mois-1), this.jour);
		DAOJoursImpl jourImpl = new DAOJoursImpl();
		List<Creneau> lc = new ArrayList();
		lc = crenImpl.findByDate(jourImpl.find(datecren));
		for(Creneau cren : lc)
		{
			heureDeb = String.valueOf(cren.getHorairedebut());
			heureFin = String.valueOf(cren.getHorairefin());

			int heureD;
			int heureF;
			int minD;
			int minF;

			if (heureDeb.length()<=3) 
			{
				heureD = Integer.parseInt(heureDeb.substring(0,1));
				minD = Integer.parseInt(heureDeb.substring(1,heureDeb.length()));
			}
			else
			{
				heureD = Integer.parseInt(heureDeb.substring(0,2));
				minD = Integer.parseInt(heureDeb.substring(2,heureDeb.length()));
			}

			if (heureFin.length()<=3)
			{
				heureF = Integer.parseInt(heureFin.substring(0,1));
				minF = Integer.parseInt(heureFin.substring(1,heureFin.length()));
			}
			else
			{
				heureF = Integer.parseInt(heureFin.substring(0,2));
				minF = Integer.parseInt(heureFin.substring(2,heureFin.length()));
			}

			if(heureD >6 && heureD < 20)
			{
				if(minD>0)
				{
					caseDeb = (heureD-7)*4+minD/15;
				}
				else
				{
					caseDeb = (heureD-7)*4;
				}

				if(minF>0)
				{
					caseFin = (heureF-7)*4+minF/15-1;
				}
				else
				{
					caseFin = (heureF-7)*4-1;
				}

				for(int j = caseDeb; j<=caseFin; j++)
				{
					if(j<47)
					{
						libelle = cren.getEc().getLibelle()+ "<br>"+cren.getEnseignant().getPrenom()+" "+cren.getEnseignant().getNom()+"<br>Salle "+cren.getSalle().getNumsalle();
						this.creneau.set(j,libelle);
					}
				}
			}
		}
	}

	public String afficherJour()
	{
		//System.out.println(this.getCreneau());
		StringBuilder  colonne = new StringBuilder();
		colonne.append("Journée du ").append(this.jour).append("/").append(this.mois).append("/").append(this.annee);
		colonne.append("<table border=1 style='border-collapse: collapse;' class='tab_aff_j'>");
		String crenCourant = "";
		boolean memeCase;
		int rowspan;
		int j = 0;

		// On parcourt chaque créneau
		for(int i = 0; i<48; i++)
		{
			memeCase = true;
			rowspan = 1;
			colonne.append("<tr style='height: 25px'>"); //ligne

			// Si le creneau est une heure, on fusionne 4 cellules
			if((i%4) == 0)
			{
				colonne.append("<td rowspan=\"4\">");
				colonne.append(i/4+7);
				colonne.append(":00</td>");
			}
			
			// Aucun créneau en cours
			if(j-i<=0)
			{
				j = i;
				while(memeCase)
				{
					crenCourant = this.creneau.get(i);
					/*System.out.println("cren cour : "+crenCourant);
					System.out.println("cren +1 : "+this.creneau.get(i+1));*/

					// Si on n'est pas à la fin du tableau
					// et que le creneau n'est pas vide
					// et qu'il est égal au suivant
					if(i<47)
					{
						if(j<47)
						{
							if(crenCourant.compareTo("") != 0)
							{
								if(crenCourant.compareTo(this.creneau.get(j+1)) == 0)
								{
									rowspan++;
									j++;
								}
								else
								{
									memeCase = false;
									j++;
								}
							}
							else
							{
								//System.out.println("Le creneau est vide");
								memeCase = false;
								crenCourant = "&nbsp;";
							}
						}
						else
						{
							//System.out.println("j est > à 47");
							memeCase = false;
							crenCourant = "&nbsp;";
						}
					}
					else
					{
						//System.out.println("i est > à 47");
						memeCase = false;
						crenCourant = "&nbsp;";
					}
				}
				colonne.append("<td rowspan=\"");
				colonne.append(rowspan);
				colonne.append("\">");
				colonne.append(crenCourant);
				colonne.append("</td>");
			}

			colonne.append("</tr>");
		}
		colonne.append("</table>");

		return colonne.toString();
	}

	public String getCreneau() {
		return creneau.toString();
	}


}
